#include <stdio.h>
#include "authentification.h"
#include <string.h>

int authentifier ()
{
    int nbr=3, test=0;
    int pswd;
    char login[20];

    printf("        Authentification        \n");

    do{
        printf(" Login : ");
        scanf("%s",login);
        printf(" Password : ");
        scanf("%d",&pswd);
        nbr--;
        if ((strcmp(login,"salah")==0)&&(pswd==1111))
        {
           test=1;

        }else if (verif(login,pswd)==1)
               {
                test=-1;
               }
       }while((test==0)&& (nbr>0));
    system("clear");

    return(test);

}

int verif (char login[],int x)
{   int test=0;
    Personnel p;
    char chemin[]="fpersonnel.txt";
    FILE *f;
    f=fopen(chemin,"r");
    if(f==NULL)
    {
        printf("Erreur !!");
    }
    else{
          while (fscanf(f,"%s %s %d %d %s" , p.nom, p.prenom, &p.age, &p.id, p.etat) != EOF)
          {
            if ((strcmp(login,p.nom)==0)&&(x==p.id))
            {
             test=1;
            }
          }
        }
    fclose(f);
    return (test);
}